package org.ns.summer.example.interceptor;

import org.ns.summer.beans.anns.Autowired;
import org.ns.summer.beans.anns.Component;
import org.ns.summer.core.AnnotationUtils;
import org.ns.summer.example.pojo.UserInfo;
import org.ns.summer.example.pojo.UserLoginResp;
import org.ns.summer.example.service.UserService;
import org.ns.summer.mvc.dynamicexec.ExecParamInfo;
import org.ns.summer.mvc.dynamicexec.ExecParamTemplate;
import org.ns.summer.mvc.mappinghandler.HandlerInterceptor;
import org.ns.summer.mvc.mappinghandler.HandlerMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * 权限校验拦截器
 */
@Component
public class AuthorityInterceptor implements HandlerInterceptor , ExecParamTemplate {
    @Autowired
    UserService userService;
    @Override
    public boolean preHandler(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod) {

        // 验证登陆
        if (handlerMethod.existAnnotation(LoginVerify.class)) {
             String token = userService.getTokeyByRequest(request);
             Long userId = userService.getUserIdByToken(token);

            if (userId == null) {
                loginFail(response);
                return false;
            }
            // 权限验证
            if (handlerMethod.existAnnotation(Security.class)) {
                Security security = handlerMethod.getAnnotation(Security.class);
                String[] roleKeys = security.value();
                if (!userService.verifyRole(roleKeys, userId)) {
                    roleFail(response);
                    return false;
                }

            }
        }
        return true;
    }

    @Override
    public Object paramLoad(ExecParamInfo autoParamInfo) throws Exception {
        HttpServletRequest request = (HttpServletRequest)autoParamInfo.getAutoParam().get(HttpServletRequest.class);
        String token = userService.getTokeyByRequest(request);
        Class<?> paramType = autoParamInfo.getParamType();
        if (paramType == String.class) {
            return token;
        }
        if (paramType == Long.class || paramType == long.class) {
            Long userId = userService.getUserIdByToken(token);
            return userId;
        }
        return null;
    }

    @Override
    public boolean preHandle(ExecParamInfo autoParamInfo) {
        return AnnotationUtils.existSuperAnnotation(autoParamInfo.getAnnotations(), Token.class);
    }

    private void loginFail(HttpServletResponse response) {
        try {
            UserLoginResp resp = userService.toLoginFail();
            PrintWriter writer = response.getWriter();
            writer.write(resp.toJson());
            writer.flush();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void roleFail(HttpServletResponse response) {
        try {
            UserLoginResp resp = userService.toRoleFail();
            PrintWriter writer = response.getWriter();
            writer.write(resp.toJson());
            writer.flush();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
